package com.wwj.core.wawa.service.impl;

import com.wwj.core.wawa.entity.PlayerItem;
import com.wwj.core.wawa.entity.PlayerItemChangeRecord;
import com.wwj.core.wawa.entity.mapper.PlayerItemChangeRecordMapper;
import com.wwj.core.wawa.entity.mapper.PlayerItemMapper;
import com.wwj.core.wawa.service.IPlayerItemService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Auser on 2017/11/4.
 */
@Service
public class PlayerItemServiceImpl implements IPlayerItemService {
    @Resource
    private PlayerItemMapper playerItemMapper;
    @Resource
    private PlayerItemChangeRecordMapper playerItemChangeRecordMapper;


    @Override
    public void updatePlayerItem(PlayerItem playerItem,String description) throws Exception {
        PlayerItem oldPlayerItem = playerItemMapper.findById(playerItem.getId());

        //更新金币数
        playerItemMapper.updatePlayerItem(playerItem);

        //添加变更记录
        PlayerItemChangeRecord record = new PlayerItemChangeRecord();
        record.setChangeTime(new Date());
        record.setChangeType(PlayerItemChangeRecord.TYPE_INCREASE);
        record.setDescription(description);
        record.setItemId(playerItem.getItemId());
        record.setItemNum(playerItem.getItemNum());
        PlayerItem newPlayerItem = playerItemMapper.findById(playerItem.getId());
        record.setNewNum(newPlayerItem.getItemNum());
        record.setOldNum(oldPlayerItem.getItemNum());
        record.setPlayerId(oldPlayerItem.getPlayerId());
        record.setSubChangeType(PlayerItemChangeRecord.IN_SUB_TYPE_MANUAL);

        playerItemChangeRecordMapper.insertPlayerItemChangeRecord(record);

    }

    @Override
    public PlayerItem findByPlayerId(int playerId) {
        return playerItemMapper.selectPlayerItemByPlayerId(playerId);
    }
}
